package com.atkxp.sys.service.impl;

import com.atkxp.sys.entity.Menu;
import com.atkxp.sys.entity.RoleMenu;
import com.atkxp.sys.mapper.MenuMapper;
import com.atkxp.sys.mapper.RoleMenuMapper;
import com.atkxp.sys.service.IMenuService;
import com.atkxp.sys.service.IRoleMenuService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author kxp
 * @since 2023-08-26
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements IMenuService {

    @Autowired
    private RoleMenuMapper roleMenuMapper;

    @Override
    public List<Menu> getAllMenu() {
        LambdaQueryWrapper<Menu> wrapper = new LambdaQueryWrapper();
        wrapper.eq(Menu::getParentId,0);
        List<Menu> menuList = this.list(wrapper);
        // 子菜单
        setMenuChildren(menuList);
        return menuList;
    }

    @Override
    public List<Menu> getMenuListByUserId(Integer userId) {
        // 一级菜单
        List<Menu> menuList = this.getBaseMapper().getMenuListByUserId(userId, 0);
        menuList.stream().distinct();
        // 子菜单
        setMenuChildrenByUserId(userId, menuList);
        return menuList;
    }

    @Override
    public List<Menu> getMenuList(String name) {
        LambdaQueryWrapper<Menu> wrapper = new LambdaQueryWrapper();
        if(StringUtils.hasText(name)) {
            wrapper.like(Menu::getTitle, name);
        } else {
            wrapper.eq(Menu::getParentId,0);
        }

        List<Menu> menuList = this.list(wrapper);
        setMenuChildren(menuList);
        return menuList;
    }

    @Override
    @Transactional
    public void deleteMenuById(Integer id) {
        Menu menu = this.baseMapper.selectById(id);
        if(menu.getMenuType().equals("M")) {
            LambdaQueryWrapper<Menu> wrapper = new LambdaQueryWrapper<>();
            LambdaQueryWrapper<RoleMenu> wrapper2 = new LambdaQueryWrapper<>();
            wrapper.eq(Menu::getParentId, id);
            List<Menu> menus = this.baseMapper.selectList(wrapper);
            List<Menu> result = new ArrayList<>();
            result.addAll(menus);
            getChildIdListById(menus, result);

            List<Integer> id_list = result.stream().map(Menu::getMenuId).collect(Collectors.toList());

            id_list.add(id);

            wrapper2.in(RoleMenu::getMenuId, id_list);
            roleMenuMapper.delete(wrapper2);

            this.baseMapper.delete(wrapper);
            this.baseMapper.deleteById(id);
        } else {
            LambdaQueryWrapper<RoleMenu> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(RoleMenu::getMenuId, id);
            roleMenuMapper.delete(wrapper);
            this.baseMapper.deleteById(id);
        }
    }

    public void getChildIdListById(List<Menu> menuList, List<Menu> result) {
        if(menuList != null) {
            for (Menu menu : menuList) {
                if(menu.getMenuType().equals("M")) {
                    LambdaQueryWrapper<Menu> wrapper = new LambdaQueryWrapper<>();
                    wrapper.eq(Menu::getParentId, menu.getMenuId());
                    List<Menu> menuList1 = this.baseMapper.selectList(wrapper);
                    result.addAll(menuList1);

                    getChildIdListById(menuList1, result);
                }
            }
        }

    }

    private void setMenuChildrenByUserId(Integer userId, List<Menu> menuList) {
        if (menuList != null) {
            for (Menu menu : menuList) {
                List<Menu> subMenuList = this.getBaseMapper().getMenuListByUserId(userId, menu.getMenuId());
                menu.setChildren(subMenuList);
                // 递归
                setMenuChildrenByUserId(userId,subMenuList);
            }
        }
    }

    private void setMenuChildren(List<Menu> menuList) {
        if(menuList != null) {
            for (Menu menu:menuList) {
                LambdaQueryWrapper<Menu> subWrapper = new LambdaQueryWrapper();
                subWrapper.eq(Menu::getParentId, menu.getMenuId());
                List<Menu> subMenuList = this.list(subWrapper);
                menu.setChildren(subMenuList);
                // 递归
                setMenuChildren(subMenuList);
            }
        }
    }
}
